

clear

* Load in data *

import excel "${path}\Raw Data\NIPA\Investment.xls", sheet("nipa_5__5__5uq") firstrow
rename T GenInd

gen year = int(quarter)
replace quarter = (quarter - year)
replace quarter =round(quarter*10)

format %ty year
format %tq quarter

save "${path}\Clean Data\NIPA.dta", replace

* Import FRED Data
import excel "${path}\Raw Data\FRED\fredgraph_Q.xls", sheet("Sheet 1") firstrow clear

gen year = year(Date)
gen quarter = quarter(Date)

merge 1:1 year quarter using "${path}\Raw Data\NIPA\NIPA.dta"
keep if _merge == 3
drop _merge

gen date = yq(year, quarter)

merge 1:1 date using  "${path}\Clean Data\Monetary Shocks\RRShocks_Q.dta"

tsset date

local list Total Nonresidentialequipment InfoProcess Computer Communication Medical ElectroMedical MedInstru NonMedInstru Photocopy OfficeAcct Industrial MetalFab Engine SteamEngine CombuEng MetalMachine SpecialIndMach GenInd ElecTran Transport Trucks Auto Aircraft Boats Rail Other Construction ConTractors ConOther Mining Service ElectricalEquip

* Put variables of interest in terms of logs *
foreach v in `list' GDP CPI {
	replace `v' = log(`v')
	*replace `v' = `v' - l.`v' if date  >= 1
}


rename resid_garch RR

*********************
* Define local macros
*********************

* number of lags
local MaxLPLags 3

* horizon
local horizon 12

* Romer shocks or Fed Funds instrumented for with Romer shocks
local policy RR

* LHS variable: the response variable *



foreach v in `list' {
forvalues i=0/`horizon' {
	
	gen `v'`i'    = f`i'.`v' - l1.`v'
	
}
}

foreach v in `list'{
gen b_`v'=.
gen se_`v'=.
local rhs`v' l(1/`MaxLPLags').FF l(1/`MaxLPLags').`v' l(1/`MaxLPLags').GDP l(1/`MaxLPLags').CPI

}

* Run the local projections regressions * 

foreach v in `list'{
forvalues i=0/`horizon' {	

	newey `v'`i' RR `rhs`v'', lag(3)
				
	replace b_`v'  = _b[RR] if _n == `i'+1
	replace se_`v' = _se[RR] if _n == `i'+1
	
}
}

* Create the confidence intervals * 

	scalar sig1 = 0.05	 // specify significance level
	scalar sig2 = 0.3	 // specify significance level
	
foreach v in `list' {

	gen up_`v' = b_`v' + invnormal(1-sig1/2)*se_`v' if _n <= (`horizon' + 1)
	gen dn_`v' = b_`v' - invnormal(1-sig1/2)*se_`v' if _n <= (`horizon' + 1)

	gen up2_`v' = b_`v' + invnormal(1-sig2/2)*se_`v' if _n <= (`horizon' + 1)
	gen dn2_`v' = b_`v' - invnormal(1-sig2/2)*se_`v' if _n <= (`horizon' + 1)
	
}

foreach v in `list'{

	
	replace b_`v' = -.25*b_`v'
	replace up_`v' = -.25*up_`v'
	replace dn_`v' = -.25*dn_`v'
	replace up2_`v' = -.25*up2_`v'
	replace dn2_`v' = -.25*dn2_`v'
	
	}
	
gen Quarters = _n-1 if _n <= `horizon' +1	
	
/* Identify responsive industries * 

gen TYPE = .
tostring(TYPE), replace
gen MAX = .
local i = 1

foreach v in `list'{
	
egen max = max(b_`v')
replace MAX = max if _n == `i'
replace TYPE ="`v'" if _n == `i'

local i = `i'+1	
drop max
	
}
replace MAX = . if TYPE == "Total" | TYPE == "Construction" | TYPE == "Transport"

sum MAX, detail
gen n = 1
sort n
by n: egen p66 = pctile(MAX), p(66)
gen TOP = 1 if MAX > p66 & TYPE != "Total"
replace TOP = . if TYPE == "."

tab TYPE if TOP == 1
	
	*/
	
	
* Plot IRFs *

foreach v in OfficeAcct Transport Construction ElectricalEquip MetalMachine CombuEng {
twoway 	(line b_`v' Quarters, lcolor(black) ///
	lpattern(solid) lwidth(medium)) /// 
	(line up2_`v' Quarters, lcolor(grey) ///
	lpattern(dash) lwidth(medium)) ///
	(line dn2_`v' Quarters, lcolor(grey) ///
	lpattern(dash) lwidth(medium)), ///
	title("Electrical Equipment", color(black) size(medium)) ///
	ylabel(-0.025(.025).05) ytitle("Log Fixed Investment in Equipment", size(medsmall)) xtitle("Quarters", size(medsmall)) ///
	graphregion(color(white)) legend(off) plotregion(color(white)) ///
	name(`v', replace)
	graph save "${path}\Results\Investment\Investment`v'.gph", replace
	
}

















